<template>
  <view class="yh-page">
	<view class="page">
		<view class="uni-column">
			<view class="yh-title">
				通过用户id登录
			</view>
			<view class="yh-title">
				这个接口仅限开发调试环境开放,生产环境是关闭的
			</view>
		</view>
		<view class="uni-form-item uni-column">
			<view class="title"><text class="red">*</text>用户id</view>
			<view class="uni-input-wrapper">
				<input class="uni-input" name="user_id" v-model="user_id" placeholder="请输入用户id"  @input="inputClearUserIdInput" />
				<text class="uni-icon" v-if="showClearUserIdIcon" @tap="handleToClearUserIdIcon">&#xe434;</text>
			</view>
			<button type="primary" @tap="handleToLoginByUserId()">执行登录</button>
		</view>
		<br/>
		<view class="uni-column">
			<view class="yh-title">
				退出
			</view>
			<view class="uni-btn-v">
				<button type="default" @tap="handleToLogOut()">退出</button>
			</view>
		</view>
		<view class="uni-column">
			<view class="yh-title">
				短信验证码注册
			</view>
			<view class="uni-btn-v">
				<button type="default" @tap="handleToRegister()">去注册</button>
			</view>
		</view>
		<view class="uni-column">
			<view class="yh-title">
				短信验证码登录
			</view>
			<view class="uni-btn-v">
				<button type="default" @tap="handleToLoginByCode()">去登录</button>
			</view>
		</view>
		<view class="uni-column">
			<view class="yh-title">
				手机号密码登录
			</view>
			<view class="uni-btn-v">
				<button type="default" @tap="handleToLoginByPassword()">去登录</button>
			</view>
		</view>
		<view class="uni-column">
			<view class="yh-title">
				微信登录授权
			</view>
			<view class="uni-btn-v">
				<button type="default" @tap="handleToLoginByWechat()">去登录</button>
			</view>
		</view>
	</view>
  </view>
</template>
<script>
import { loginByUserId }  from '@/api/test/login/login.js'
import { logout }  from '@/api/test/after-login/after-login.js'
import { validNumber } from '@/utils/validate'
export default 
{
  //计算属性
  computed:
  {

  },
  //监听
  watch:
  {

  },
  data() 
  {
    return{
		//用户id
		user_id:null,
		//输入框是否显示用户id标识
		showClearUserIdIcon:false
    };
  },
  //页面加载
  onLoad() 
  {

  },
  //页面显示
  onShow() 
  {

  },
  //页面初次渲染完成
  onReady()
  {

  },
  //页面隐藏
  onHide()
  {

  },
  //页面卸载
  onUnload()
  {

  },
  methods:
  {
	//清除用户id输入
	inputClearUserIdInput()
	{
	    if (this.user_id.length > 0) 
		{
	        this.showClearUserIdIcon = true;
	    } 
		else 
		{
	        this.showClearUserIdIcon = false;
	    }
	},
	//用户点击清空标识
	handleToClearUserIdIcon() 
	{
		this.user_id = null;
	    this.showClearUserIdIcon = false;
	},
	//用户id登录
	async handleToLoginByUserId()
	{
		let validatedNumber =  validNumber(this.user_id)
		
		if(!validatedNumber)
		{
			uni.showToast({
				image: "/static/test/notice/04-error.png",
				title: "必须是数字",
				mask:true,
				duration:2000
			})
			
			return 
		}
		
		let _this = this
		let param = {}
		param['user_id'] = this.user_id
		const result = await loginByUserId(param).then(res=>
		{
			return new Promise((resolve,reject)=>{
				if(res && res.code == 0)
				{
					resolve(res.data)
				}
			})
		})
		const {token,openid} = result
		
		if(token)
		{
			_this.$store.dispatch('user/loginSaveToken',token)
			
			if(openid)
			{
				_this.$store.dispatch('user/authSaveOpenid',openid)
			}
			
			uni.switchTab({
				url:'/pages/Test/V1/tabBar/index/index'
			})
		}
	},
	//退出
	async handleToLogOut()
	{
		const result = await logout().then(res=>
		{
			return new Promise((resolve,reject)=>{
				if(res && res.code == 0)
				{
					resolve(true)
				}
				else
				{
					reject(false)
				}
			})
		})
		
		console.log(result)
		
		if(result)
		{
			this.$store.dispatch('user/logout')
		}
	},
	//去注册 
	handleToRegister()
	{
		uni.navigateTo({
			url:'/pages/Test/V1/login/phone-register/phone-register'
		})
	},
	//短信验证码登录
	handleToLoginByCode()
	{
		uni.navigateTo({
			url:'/pages/Test/V1/login/phone-code-login/phone-code-login'
		})
	},
	//手机号密码登录
	handleToLoginByPassword()
	{
		uni.navigateTo({
			url:'/pages/Test/V1/login/phone-password-login/phone-password-login'
		})
	},
	//微信授权登录
	handleToLoginByWechat()
	{
		uni.switchTab({
			url:'/pages/Test/V1/tabBar/wechat-auth/wechat-auth'
		})
	}
  }

}
</script>

<style lang="scss" scoped>
.page{
	width:calc(100% - 70rpx);
	padding:35rpx;
}
.title {
        padding: 5rpx 13rpx;
    }

.uni-input-wrapper {
	/* #ifndef APP-NVUE */
	display: flex;
	/* #endif */
	flex-direction: row;
	flex-wrap: nowrap;
	background-color: #FFFFFF;
}

.uni-input {
	height: 80rpx;
	line-height: 80rpx;
	font-size: 36rpx;
	padding: 0rpx;
	flex: 1;
	background-color: #FFFFFF;
}

.uni-icon {
	font-family: uniicons;
	font-size: 80rpx;
	font-weight: normal;
	font-style: normal;
	width: 80rpx;
	height: 80rpx;
	line-height: 80rpx;
	color: #999999;
}

.uni-eye-active {
	color: #007AFF;
}

.red{
	color:red;
	font-weight: true;
}
</style>
